package tests;

import static org.junit.Assert.*;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import model.Email;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;


/**
 * This class tests the Email class.
 * 
 * 
 * @author b_aldosa
 *
 */
public class TestEmail {

	Email SUT;
	
	/**
	 * This method is called before every test case to
	 * initialize the Email obejct.
	 */
	@Before
	public void setUp() {
		SUT=new Email();
	}
	/**
	 * This method is called after every test case to nullify the email onject.
	 */
	@After
	public void tearDown()
	{
		SUT=null;
	}
	/**
	 * This test case tests the Has No More Pending Recepients method
	 * for both cases
	 */
	@Test
	public void testHasNoMorePendingRecepients() {
		SUT.setPendingRecepients("");
		assertTrue(SUT.hasNoMorePendingRecepients());
		SUT.setPendingRecepients("musab, bee");
		assertFalse(SUT.hasNoMorePendingRecepients());
	}
	/**
	 * This test case tests the Remove Pending recipients method
	 */
	@Test
	public void testRemovePendingRecepient() {
		SUT.setPendingRecepients("bee, musab");
		SUT.removePendingRecepient("bee");
		assertEquals(" musab",SUT.getPendingRecepients());
	}
	/**
	 * This test case tests the has pending recipients method
	 */
	@Test
	public void testHasPendingRecepient() {
	SUT.setPendingRecepients("bee");
	assertTrue(SUT.hasPendingRecepient("bee"));
	}
	/**
	 * This test case tests the get pending recipeints method
	 */
	@Test
	public void testGetPendingRecepients() {
		SUT.setPendingRecepients("bee,musab");
		assertEquals("bee,musab",SUT.getPendingRecepients());
	}
	/**
	 * This test case tests the set pending recipients method
	 */
	@Test
	public void testSetPendingRecepients() {
		SUT.setPendingRecepients("bee,musab");
		assertEquals("bee,musab",SUT.getPendingRecepients());
	}
	/**
	 * This test case tests the get id method
	 */
	@Test
	public void testGetId() {
		assertEquals(-1,SUT.getId());
	}
	/**
	 * This test case tests the get to method
	 */
	@Test
	public void testGetTo() {
		SUT.setTo("Sk");
		assertEquals("Sk",SUT.getTo());
		
	}
	/**
	 * This test case tests the get from method
	 */
	@Test
	public void testGetFrom() {
		SUT.setFrom("Sk");
		assertEquals("Sk",SUT.getFrom());
	}
	/**
	 * This test case tests the get subejct method
	 */
	@Test
	public void testGetSubject() {
		SUT.setSubject("Hello");
		assertEquals("Hello",SUT.getSubject());
	}
	/**
	 * This test case tests the get body method
	 */
	@Test
	public void testGetBody() {
		SUT.setBody("Body");
		assertEquals("Body",SUT.getBody());
	}
	/**
	 * This test case tests the is read method
	 */
	@Test
	public void testIsRead() {
		SUT.setRead(false);
		assertFalse(SUT.isRead());
	}
	/**
	 * This test case tests the get timestamp method
	 */
	@Test
	public void testGetTimestamp() {
		Date date= new Date();
		Timestamp currentTime=(new Timestamp(date.getTime()));
		SUT.setTimestamp(currentTime);
		assertEquals(currentTime,SUT.getTimestamp());
	}
	/**
	 * This test case tests the set id method
	 */
	@Test
	public void testSetId() {
		SUT.setId(0);
		assertEquals(0,SUT.getId());
	}
	/**
	 * This test case tests the set to method
	 */
	@Test
	public void testSetTo() {
		SUT.setTo("to");
		assertEquals("to",SUT.getTo());
	}
	/**
	 * This test case tests the set from method
	 */
	@Test
	public void testSetFrom() {
		SUT.setFrom("from");
		assertEquals("from",SUT.getFrom());
	}
	/**
	 * This test case tests the set subject method
	 */
	@Test
	public void testSetSubject() {
		SUT.setSubject("subject");
		assertEquals("subject",SUT.getSubject());
	}
	/**
	 * This test case tests the set body method
	 */
	@Test
	public void testSetBody() {
		SUT.setBody("body");
		assertEquals("body",SUT.getBody());
	}
	/**
	 * This test case tests the set read method
	 */
	@Test
	public void testSetRead() {
		SUT.setRead(true);
		assertTrue(SUT.isRead());
	}
	/**
	 * This test case tests the set timestamp method
	 */
	@Test
	public void testSetTimestamp() {
		Date date= new Date();
		Timestamp currentTime=(new Timestamp(date.getTime()));
		SUT.setTimestamp(currentTime);
		assertEquals(currentTime,SUT.getTimestamp());
	}
	/**
	 * This test case tests the get transfer object method
	 */
	@Test
	public void testGetTransferObject() {
		HashMap<String, String> result=SUT.getTransferObject();
		assertEquals(result.getClass().toString(),"class java.util.HashMap");
	}
	/**
	 * This test case tests the to string method
	 */
	@Test
	public void testToString() {
		String toReturn = "\n\n------------ email start ----------------\n";
		toReturn += "id: " + SUT.getId() + "\n" +
				"to: " + SUT.getTo() + "\n" + 
				"from: " + SUT.getFrom() + "\n" + 
				"subject: " + SUT.getSubject() + "\n" +
				"body" + SUT.getBody() +  "\n" + 
				"read: " + SUT.isRead() +  "\n" + 
				"ts" + SUT.getTimestamp() +  "\n" + 
				"pendingRecepients: " + SUT.getPendingRecepients() +  "\n" +
				"--------- email end ----------------------";
		assertEquals(toReturn,SUT.toString());
	}

	/**
	 * This test case tests the match method
	 */
	@Test
	public void testMatch() {
		SUT.setTo("to");
		assertEquals(true,SUT.match("to"));
	}
	/**
	 * This test case tests the has recipient method
	 */
	@Test
	public void testHasRecipient() {
		SUT.setTo("musab,bee");
		assertTrue(SUT.hasRecipient("bee"));
	}
	/**
	 * This test case tests the get pending recipient list method
	 */
	@Test
	public void testGetPendingRecepientsList() {
		SUT.setPendingRecepients("musab,bee");
		ArrayList<String> result=new ArrayList<String>();
		result.add("musab");
		result.add("bee");
		assertEquals(result,SUT.getPendingRecepientsList());
	}

}
